//
//  TextViewExt.swift
//  kano
//
//  Created by Frank on 2025/5/20.
//


import UIKit

extension UITextView {
    func setPlaceholder(text: String, color: UIColor) {
        placeholderLabel.text = text
        placeholderLabel.textColor = color
    }
    
    private struct AssociatedKeys {
        static var placeholderLabel = "placeholderLabel"
    }
    
    var placeholderLabel: UILabel {
        get {
            if let label = objc_getAssociatedObject(self, &AssociatedKeys.placeholderLabel) as? UILabel {
                return label
            }
            
            let label = UILabel()
            label.font = font
            label.textColor = .placeholderColor
            label.isHidden = !text.isEmpty
            addSubview(label)
            
            label.snp.makeConstraints { make in
                make.top.left.equalToSuperview().inset(8)
            }
            
            objc_setAssociatedObject(
                self,
                &AssociatedKeys.placeholderLabel,
                label,
                .OBJC_ASSOCIATION_RETAIN_NONATOMIC
            )
            
            return label
        }
        set {
            objc_setAssociatedObject(
                self,
                &AssociatedKeys.placeholderLabel,
                newValue,
                .OBJC_ASSOCIATION_RETAIN_NONATOMIC
            )
        }
    }
}
